package com.example.administrator.managesystem.activity.my_profile.typein;

import android.content.Context;
import android.util.Log;

import com.example.administrator.managesystem.bean.ScanGoodsBean;
import com.example.administrator.managesystem.bean.TagBean;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.ui.AddLandView;
import com.example.administrator.managesystem.utils.ShareUtil;
import com.example.administrator.managesystem.utils.ToastUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Eason
 * on 2019/11/19
 */
public class TypePresenter {
    private AddLandView addLandView;

    public TypePresenter(AddLandView addLandView) {
        this.addLandView = addLandView;
    }

    public void goodsTypeRequest(final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).goodsTypeRequest()
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<TagBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(TagBean tagBean) {
                        if(tagBean.getStatus()==200){
                            addLandView.showLandTag(tagBean.getData());
                        }
                    }

                });


    }

    public void uploadTraces(final Context context, String category_id, String userName, String phone, String address, String date, String code, int number, List<String> list){
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        //.addFormDataPart(ParamKey.TOKEN, token);//ParamKey.TOKEN 自定义参数key常量类，即参数名
        //多张图片
        for (int i = 0; i < list.size(); i++) {
            File file = new File(list.get(i));//filePath 图片地址
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("images", file.getName(), imageBody);//"imgfile"+i 后台接收图片流的参数名
        }
        List<MultipartBody.Part> parts = builder.build().parts();

        ServiceManager.getInstance(context).getService(GuideAPI.class).uploadTracks(ShareUtil.getUserId(context),ShareUtil.getUserToken(context),code,number,category_id,userName,phone,address,date,parts)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<ScanGoodsBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(ScanGoodsBean scanGoodsBean) {

                     ToastUtils.showToast(context,scanGoodsBean.getData().getMsg());
                 }


                });


    }
}
